package com.squareup.picasso;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.SystemClock;
import android.widget.ImageView;

import com.squareup.picasso.Picasso.LoadedFrom;

public final class PicassoDrawable extends BitmapDrawable {
    private static final Paint DEBUG_PAINT = new Paint();
    private final boolean debugging;
    private final float density;
    private final LoadedFrom loadedFrom;
    private Drawable placeholder;
    private long startTimeMillis;
    private boolean animating;
    private int alpha = 255;

    private PicassoDrawable(Context context, Bitmap bitmap, Drawable drawable, LoadedFrom loadedFrom, boolean noFade, boolean debugging) {
        super(context.getResources(), bitmap);
        this.debugging = debugging;
        this.density = context.getResources().getDisplayMetrics().density;
        this.loadedFrom = loadedFrom;
        int v0 = loadedFrom != LoadedFrom.MEMORY ? 1 : 0;
        if (v0 != 0) {
            this.placeholder = drawable;
            this.animating = true;
            this.startTimeMillis = SystemClock.uptimeMillis();
        }
    }

    private static Path getTrianglePath(Point p1, int width) {
        Point p2 = new Point(p1.x + width, p1.y);
        Point p3 = new Point(p1.x, p1.y + width);
        Path path = new Path();
        path.moveTo(((float) p1.x), ((float) p1.y));
        path.lineTo(((float) p2.x), ((float) p2.y));
        path.lineTo(((float) p3.x), ((float) p3.y));
        return path;
    }

    static void setBitmap(ImageView imageView, Context context, Bitmap bitmap, LoadedFrom loadedFrom, boolean debugging) {
        Drawable placeholder = imageView.getDrawable();
        if ((placeholder instanceof AnimationDrawable)) {
            ((AnimationDrawable) placeholder).stop();
        }
        imageView.setImageDrawable(new PicassoDrawable(context, bitmap, placeholder, loadedFrom, false, debugging));
    }

    static void setPlaceholder(ImageView imageView, Drawable placeholderDrawable) {
        imageView.setImageDrawable(placeholderDrawable);
        if ((imageView.getDrawable() instanceof AnimationDrawable)) {
            ((AnimationDrawable) imageView.getDrawable()).start();
        }
    }

    public final void draw(Canvas canvas) {
        if (!this.animating) {
            super.draw(canvas);
        } else {
            float normalized = (float) (SystemClock.uptimeMillis() - this.startTimeMillis) / 200f;
            if (normalized >= 1f) {
                this.animating = false;
                this.placeholder = null;
                super.draw(canvas);
            } else {
                if (this.placeholder != null) {
                    this.placeholder.draw(canvas);
                }
                super.setAlpha(((int) (normalized * (((float) this.alpha)))));
                super.draw(canvas);
                super.setAlpha(this.alpha);
            }
        }

        if (this.debugging) {
            PicassoDrawable.DEBUG_PAINT.setColor(-1);
            canvas.drawPath(PicassoDrawable.getTrianglePath(new Point(0, 0), ((int) (16f * this.density))), PicassoDrawable.DEBUG_PAINT);
            PicassoDrawable.DEBUG_PAINT.setColor(this.loadedFrom.debugColor);
            canvas.drawPath(PicassoDrawable.getTrianglePath(new Point(0, 0), ((int) (15f * this.density))), PicassoDrawable.DEBUG_PAINT);
        }
    }

    protected final void onBoundsChange(Rect bounds) {
        if (this.placeholder != null) {
            this.placeholder.setBounds(bounds);
        }
        super.onBoundsChange(bounds);
    }

    public final void setAlpha(int alpha) {
        this.alpha = alpha;
        if (this.placeholder != null) {
            this.placeholder.setAlpha(alpha);
        }
        super.setAlpha(alpha);
    }

    public final void setColorFilter(ColorFilter cf) {
        if (this.placeholder != null) {
            this.placeholder.setColorFilter(cf);
        }

        super.setColorFilter(cf);
    }
}

